package edu.whu.irlab.woscrawl.doi;

import com.google.common.base.Strings;
import edu.whu.irlab.woscrawl.util.WebDriverFactory;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static edu.whu.irlab.woscrawl.util.HttpDownloader.download;

/**
 * Created by zhongwei on 2016/12/28.
 */
public class Main {
    static WebDriver driver = WebDriverFactory.getWebDriver();
    static FileWriter fileWriter = null;

    public static void main(String[] args) throws IOException {
        fileWriter = new FileWriter("records.txt", true);
        search(driver);
        int totalPages = getTotalPages();
        navigatePage(3070);
        for (int i = 3070; i < totalPages; i++) {
            // TODO process
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            processPage();
            tryClick();
        }
        fileWriter.close();
    }
    public static void search(WebDriver driver) {
        driver.get("http://apps.webofknowledge.com/UA_GeneralSearch_input.do?product=UA&search_mode=GeneralSearch&SID=U2FP8cb3RroGapsH1na&preferencesSaved=");
        driver.findElements(By.className("search-criteria-input")).get(1).clear();
        driver.findElements(By.className("search-criteria-input")).get(1).sendKeys("PLOS one");
        driver.findElements(By.className("select2-choice")).get(0).click();
        driver.findElement(By.id("select2-result-label-21")).click();
        driver.findElement(By.id("periodRange")).click();
        driver.findElement(By.id("s2id_autogen4")).click();
        System.out.println(driver.getPageSource());
        WebElement startYearEle = driver.findElement(By.name("startYear"));
        Select select = new Select(startYearEle);
        List<WebElement> options = select.getOptions();
        for (WebElement option : options) {
            if (option.getAttribute("value").equals("2013"))
                option.click();
        }
        WebElement endYear = driver.findElement(By.name("endYear"));
        select = new Select(endYear);
        options = select.getOptions();
        for (WebElement option : options) {
            if (option.getAttribute("value").equals("2013"))
                option.click();
        }
        driver.findElement(By.id("UA_GeneralSearch_input_form_sb")).click();
    }
    static void tryClick() {
        try {
            getNextEle().click();
        } catch (Exception e) {
            tryClick();
        }
    }
    static int getTotalPages() {
        try {
            String text = driver.findElement(By.id("pageCount.top")).getText();
            text = text.trim().replace(",", "");
            return Integer.parseInt(text);
        } catch (Exception ex) {
            try {
                Thread.currentThread().sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return getTotalPages();
        }
    }
    static WebElement getNextEle() {
        return driver.findElements(By.className("paginationNext")).get(0);
    }
    static void processPage() {
        try {
            WebElement element = driver.findElement(By.className("search-results"));
            List<WebElement> list = element.findElements(By.className("search-results-item"));
            for (WebElement webElement : list) {
                WebElement aEle = webElement.findElement(By.className("smallV110"));
                String link = aEle.getAttribute("href");
                while (Strings.isNullOrEmpty(webElement.getText())) {
                    Thread.sleep(1000);
                }
                System.out.println(webElement.getText());
                fileWriter.write(webElement.getText() + "\n");
                System.out.println(link);
                fileWriter.write(link + "\n");
                System.out.println("==========");
                fileWriter.write("==========" + "\n");
            }
            fileWriter.flush();
        } catch (Exception ex) {
            try {
                Thread.currentThread().sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            processPage();
        }
    }
    static void getDOI(File outputDir) throws IOException {
        Set<String> doneIDs = new HashSet<String>();
        File[] files = outputDir.listFiles();
        for (File file : files) {
            doneIDs.add(file.getName());
        }
        String content = FileUtils.readFileToString(new File("records.txt"));
        String[] arr = content.split("==========");
        for (String str : arr) {
            String[] lines = str.split("\n");
            int lineNums = lines.length;
            String id = lines[0] + "txt";
            if (doneIDs.contains(id))
                continue;
            FileWriter fileWriter = new FileWriter(new File(outputDir, id));
            String url = lines[lineNums - 1];
            String detail = download(url);
            Document document = Jsoup.parse(detail);
            Element element = document.getElementsByClass("l-content").get(0);
            fileWriter.write(element.text());
            fileWriter.flush();
            fileWriter.close();
        }
    }
    static void navigatePage(int start) {
        WebElement input = driver.findElement(By.className("goToPageNumber-input"));
        input.clear();
        input.sendKeys(String.valueOf(start + 1));
        input.submit();
    }
}
